package com.yanqu.road.server.timer.chuhan;

import com.yanqu.road.server.manager.activity.chuhan.CrossChuHanMgr;
import com.yanqu.road.server.manager.activity.chuhan.activity.CrossChuHanActivity;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.Map;

/**
 * 放统兵演武一些不需要及时的定时任务
 */
public class CrossChuHanTimerTask extends GeneralTask {

    private long lastCalcBattacks = 0L;

    private long lastPrintTime = 0L;

    @Override
    public void doRun() {

        long now = System.currentTimeMillis();

        // 【计算全部兵营】
        if (now - lastCalcBattacks > DateHelper.MINUTE_MILLIONS) {
            lastCalcBattacks = now;
            for (Map<Integer, CrossChuHanActivity> map : CrossChuHanMgr.getActivityGroupMap().values()) {
                for (CrossChuHanActivity activity : map.values()) {
                    if (activity.getConfig().inEnterActivityTime()) {
                        try {
                            activity.calcAllBarracks();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        // 【打印】
        if (now - lastPrintTime > DateHelper.MINUTE_MILLIONS) {
            lastPrintTime = now;
            for (Map<Integer, CrossChuHanActivity> map : CrossChuHanMgr.getActivityGroupMap().values()) {
                for (CrossChuHanActivity activity : map.values()) {
                    try {
                        activity.printTask();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        for (Map<Integer, CrossChuHanActivity> map : CrossChuHanMgr.getActivityGroupMap().values()) {
            for (CrossChuHanActivity activity : map.values()) {
                try {
//                    activity.makeNpcEvent();
                    activity.makePosSliver();
                    activity.syncRedPacketBoxValue();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        // 【自动发城池采集奖励】
        for (Map<Integer, CrossChuHanActivity> map : CrossChuHanMgr.getActivityGroupMap().values()) {
            for (CrossChuHanActivity activity : map.values()) {
                try {
                    activity.mailSendCityCollect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
